package com.itheima.mp.domain.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class PageQuery {
    private Integer pageNo;
    private Integer pageSize;
    private String sortBy;
    private Boolean isAsc;

    public <T> Page<T> toMpPage(OrderItem... orders) {
        Page<T> p = Page.of(pageNo, pageSize);
        if (sortBy != null) {
            // 处理 isAsc 为 null 的情况，设置默认升序（可根据需求调整默认值）
            boolean ascFlag = isAsc != null ? isAsc : true;
            OrderItem orderItem = ascFlag ? OrderItem.asc(sortBy) : OrderItem.desc(sortBy);
            p.addOrder(orderItem);
            return p;
        }
        if (orders != null) {
            p.addOrder(orders);
        }
        return p;
    }

    public <T> Page<T> toMpPage(String defaultSortBy, boolean isAsc) {
        OrderItem orderItem = isAsc ? OrderItem.asc(defaultSortBy) : OrderItem.desc(defaultSortBy);
        return this.toMpPage(orderItem);
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTimeDesc() {
        return toMpPage("create_time", false);
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTimeDesc() {
        return toMpPage("update_time", false);
    }
}